// <![CDATA[
$.Class.extend("dataTablesCrud",
// static properties
{
  count: 0,
  bBindButton: false,
  enabledButton: ['data', 'new'],
  rowEnabledButton: ['edit', 'delete', 'custom'],
  rowLink: []
},
// prototype properties
{
  // constructor function
  init : function(psettings){
    //save the name
    this.settings = psettings;
    this.selected_id = null;
    this.data_tables = null;
    this.createTable();
    this.fnInitCrudActions();
  },
  
  createTable : function(){
   var that = this;
   this.data_tables =  $("#"+this.settings.id).dataTable({
          'bServerSide': true,
          'bLengthChange': true,
          'sPaginationType':'full_numbers', 
          'sAjaxSource': this.settings.sAjaxSource,
          "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
            if (aData[0] == that.fnGetSelected(this))
            {
              $(nRow).addClass('row_selected');
            }
            return nRow;
          },
          "aoColumns": this.settings.aoColumns
	}).fnSetFilteringDelay();
	
	// add row listener
	$('#'+this.settings.id+' tbody tr').live('click', function(event) {
	    var aData = that.data_tables.fnGetData(this);
	    that.fnSetSelectedId(aData[0]);
	    $(that.data_tables.fnSettings().aoData).each(function (){
	      $(this.nTr).removeClass('row_selected');
	    });
	    $(event.target.parentNode).addClass('row_selected');
	    
	    // bind CrudButtons
	    if(dataTablesCrud.bBindButton == false){
	    	dataTablesCrud.bBindButton = true;
	    	that.fnBindRowEnabledControls();	
	    }
	    
	});  
  },
  
  fnBindRowEnabledControls : function() {
  	var crudActions = this.settings.crudButtons;
  	var that = this;
  	$.each(crudActions, function(){
  		if($.inArray(this.type, dataTablesCrud.rowEnabledButton) != -1)
  		{
  			if(dataTablesCrud.rowLink[this.type] == "")
  				$("#"+this.id).attr('href', 'javascript:void(0)');
  			else
  				$("#"+this.id).attr('href', dataTablesCrud.rowLink[this.type]);
  			
  			if(typeof this.callBack != 'undefined')
        		$("#"+this.id).live('click', this.callBack);
  		}
  	});
  },
  
  fnInitCrudActions : function() {
  	var that = this;
  	var crudActions = this.settings.crudButtons;
    $.each(crudActions, function() {
        
        // remove href attribute of defined controls
        if($.inArray(this.type, dataTablesCrud.enabledButton) == -1) {
        	// save the old href
        	dataTablesCrud.rowLink[this.type] = $("#"+this.id).attr('href'); 
        	$("#"+this.id).removeAttr('href');
        }
        	
        else{
        	// bind callbacks if defined
        	if(typeof this.callBack != 'undefined')
        		$("#"+this.id).live('click', this.callBack);	
        }
        
    });  	
  	
  },
  
  fnDraw : function() {
	  this.selected_id = "";
	  this.fnInitCrudActions();
	  dataTablesCrud.bBindButton = false;
	  this.data_tables.fnDraw();
  },
  
  fnSetSelectedId : function(id) {
  	this.selected_id = id;
  },
  
  fnGetSelectedId : function() {
  	return this.selected_id;
  },
  
  fnGetSelected : function( oTableLocal ){
    var aReturn = new Array();
    var aTrs = oTableLocal.fnGetNodes();

    for ( var i=0 ; i<aTrs.length ; i++ )
    {
        if ( $(aTrs[i]).hasClass('row_selected') )
        {
            aReturn.push( aTrs[i] );
        }
    }
    return aReturn;
  }
  
});


jQuery.fn.dataTableExt.oApi.fnSetFilteringDelay = function ( oSettings, iDelay ) {
  var _that = this;
  this.each( function ( i ) {
    $.fn.dataTableExt.iApiIndex = i;
    var	iDelay = (iDelay && (/^[0-9]+$/.test(iDelay)) ? iDelay : 250),
    $this = this,  oTimerId = null, sPreviousSearch = null,
    anControl = $( 'div.dataTables_filter input:text' );
    anControl.unbind( 'keyup' ).bind( 'keyup', function() {
    var $$this = $this;
    if (sPreviousSearch === null || sPreviousSearch != anControl.val()) {
      window.clearTimeout(oTimerId);
      sPreviousSearch = anControl.val();
      oTimerId = window.setTimeout(function() {
        $.fn.dataTableExt.iApiIndex = i;
        _that.fnFilter( anControl.val() );
        }, iDelay);
      }
    });
    return this;
    } );
  return this;
};

// ]]>